home *** CD-ROM | disk | FTP | other *** search
/ Palm Utilities / Palm_Utilities_CD-ROM_2001_2001.iso / files / internet misc / GetTLE 1.0 / GetTLE.exe / Src / debug.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-04-18  |  5.3 KB  |  197 lines

  1. /*
  2.     GetTLE - MemoDebug.c - routines to write debug output to a Memo
  3.     Copyright ⌐2000 Andreas Schneider
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation; either version 2 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18. */
  19.  
  20. #ifdef linux
  21. #include <stdarg.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #else
  25. #include <PalmOS.h> 
  26. #include <unix_stdarg.h> // that's not what the documentation says...
  27. #define vsprintf StrVPrintF
  28. #define strcat StrCat
  29. #endif
  30.  
  31. #include "debug.h"
  32.  
  33. #ifdef DEBUGGING  // no need to have code we don't use
  34.  
  35. // Structures imposed by other programs
  36.  
  37. typedef struct {
  38.     char     note;        // null terminated string
  39. } MemoDBRecordType;
  40.  
  41. typedef MemoDBRecordType * MemoDBRecordPtr;
  42.  
  43. #define MemoDBName "MemoDB"
  44. #define MemoDBType 'DATA'
  45.  
  46. #define MAX_DEBUG_LENGTH 4000 // limit output
  47.  
  48. #ifndef linux
  49. // we use one large buffer. Messages get appended to it and
  50. // only at the very end is the information stored in the Memo
  51. static char debug_message[MAX_DEBUG_LENGTH]="";
  52. #endif
  53.  
  54. // a switch to turn logging on and off
  55. static short debugging_enabled=0;
  56. // and a way to find out at the end if we need to create a memo
  57. static short got_messages=0;
  58.  
  59. // append a text message to the DebugMessage[] buffer
  60. extern void LogMessage(char *format,...)
  61. {
  62.   va_list args;
  63.  
  64.   if (debugging_enabled)
  65.   {
  66.     va_start(args,format);
  67.     got_messages=1;
  68.     VLogMessage(format,args);
  69.     va_end(args);
  70.   }
  71.   return;
  72. }
  73.  
  74. extern void VLogMessage(char *format,va_list args)
  75. {
  76.   char message[150]=""; // seems a reasonable limit - I'll not do checks
  77.   unsigned long length;
  78.     #ifndef linux
  79.   static unsigned long total_length=1; // count the trailing 0 right from the start
  80.     #endif
  81.   
  82.   // vsprintf returns length of string
  83.   length=vsprintf(message,format,args);
  84. #ifdef linux
  85.   fputs(message,stderr);
  86. #else
  87.   // make sure we don't overrun the buffer
  88.   if (total_length+length<MAX_DEBUG_LENGTH)
  89.   {
  90.     // no overrun problems - append message to buffer
  91.     strcat(debug_message,message);
  92.     // and keep track of total size of buffer so far
  93.     total_length+=length;
  94.   }  
  95. #endif
  96.   return;
  97. }
  98.  
  99. // must be called first   
  100. extern void InitDebugMessages(void)
  101. {
  102. #ifndef linux
  103.   UInt32 Seconds;
  104.   DateTimeType DateTime;
  105.   char Message[64]="";
  106.   char DateString[dateStringLength];
  107.   char TimeString[timeStringLength];
  108.   
  109.   // get the current time
  110.   Seconds=TimGetSeconds();
  111.   // and convert it to a proper Date/Time combination
  112.   TimSecondsToDateTime(Seconds,&DateTime);
  113.   // Create the Memo title
  114.   // Start with the word 'DEbug'
  115.   StrCat(Message,"Debug ");
  116.   // then append the date
  117.   DateToAscii(DateTime.month,DateTime.day,DateTime.year,dfDMYWithDashes,DateString);
  118.   StrCat(Message,DateString);
  119.   // and finally the time
  120.   StrCat(Message,"  ");
  121.   TimeToAscii(DateTime.hour,DateTime.minute,tfColon24h,TimeString);
  122.   StrCat(Message,TimeString);
  123.   // terminate with a new line
  124.   StrCat(Message,"\n");
  125.   // and store it as the first message in DebugMessage[]
  126.   StrCopy(debug_message,Message);
  127. #endif
  128.   return;
  129. }
  130.  
  131. // We also want a way to start and stop debug messages
  132. extern void StartDebugMessages(void)
  133. {
  134.   debugging_enabled=true;
  135. }
  136.  
  137. extern void StopDebugMessages(void)
  138. {
  139.   debugging_enabled=true;;
  140. }
  141.  
  142. // when we're finished with the program we write the debug
  143. // information to a Memo
  144. extern void WriteDebugMessages(void)
  145. {
  146. #ifndef linux
  147.   DmOpenRef MemoDB=0;
  148.   UInt16 index=1000;
  149.   MemHandle recordHandle;
  150.   MemoDBRecordPtr recordPtr;
  151.   Err result;
  152.   UInt32 length;
  153.   
  154.   // open the database of the built-in Memo application
  155.   MemoDB = DmOpenDatabaseByTypeCreator(MemoDBType, 'memo', dmModeReadWrite);
  156.   if (MemoDB && got_messages) // only create a new memo if there are messages
  157.   {
  158.     // success - allocate memory for the new memo
  159.     length=(UInt32)StrLen(debug_message);
  160.       recordHandle=(MemHandle)DmNewHandle(MemoDB,(UInt32)(length+1)); // +1 - the trailin 0
  161.        if (recordHandle!=NULL)
  162.        {
  163.          // copy the DebugMessage[] into the new record
  164.          recordPtr=MemHandleLock(recordHandle);
  165.          DmStrCopy(recordPtr,0,debug_message);
  166.          MemPtrUnlock(recordPtr);
  167.          // and attach the record to the database
  168.          result=DmAttachRecord(MemoDB,&index,recordHandle,0);
  169.            if (result)
  170.            {
  171.              // we can now free the record Handle
  172.              MemHandleFree(recordHandle);
  173.            }
  174.        }
  175.        else
  176.        {
  177.          // We could display "Can't get new handle" here, but we don't
  178.        }
  179.   }
  180.   else
  181.   {
  182.     // "Can't find memo database";
  183.   }
  184. #endif
  185.   return;
  186. }
  187.  
  188. #else
  189.  
  190. // because of the varargs I'll need an empty implementation of LogMessage
  191.  
  192. extern void LogMessage(char *format,...)
  193. {
  194.   return;
  195. }
  196. #endif // DEBUGGING
  197.